﻿using bat_sym.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace bat_sym.SimulationLogic
{
    public class Statistics
    {
        public int _liveComp;
        public int _liveCoop;

        public List<Point> _comp;
        public List<Point> _coop;
        public List<Point> _total;

        public List<Point> _avgFood;

        public int iteration;

        public Statistics()
        {
            _comp = new List<Point>();
            _coop = new List<Point>();
            _total = new List<Point>();
            _avgFood = new List<Point>();
            iteration = 0;
        }

        public void SaveStats(ObservableCollection<Bat> simulationSpace)
        {
            if (simulationSpace.Count != 0)
            {
                UpdateLiveData(simulationSpace);
            }
        }

        private void UpdateLiveData(ObservableCollection<Bat> simulationSpace)
        {
            _liveCoop = simulationSpace.Where(x => x.Behaviour == BatBehaviour.Cooperative && x.alive).Count();
            _liveComp = simulationSpace.Where(x => x.Behaviour == BatBehaviour.Cheating && x.alive).Count();

            _comp.Add(new Point(iteration, 300 - _liveComp));

            _coop.Add(new Point(iteration, 300 - _liveCoop));

            _total.Add(new Point(iteration, 300 - (_liveComp + _liveCoop)));

            var avgfood = simulationSpace.Select(x => x.FoodAmmount).Average();
            _avgFood.Add(new Point(iteration, 300 - avgfood));

            iteration++;
        }

        public void Reset()
        {
            _comp = new List<Point>();
            _coop = new List<Point>();
            _total = new List<Point>();
            _avgFood = new List<Point>();
            iteration = 0;
            _liveComp = 0;
            _liveCoop = 0;
        }
    }
}
